package HelperFinancial.User;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import HelperFinancial.DataBase.CategoriaAdapter;
import HelperFinancial.DataBase.ConfigurationAdapter;
import HelperFinancial.DataBase.CreditoAdapter;
import HelperFinancial.DataBase.DebitoAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class BalancoActivity extends Activity {

	TextView categorias;
	TextView gastos;
	TextView ganhos;
	TextView valorGanhos;
	TextView saldo;
	DebitoAdapter debito;
	CreditoAdapter credito;
	CategoriaAdapter categoria;
	private BalancoActivity frame = this;
	private DecimalFormat fmt = new DecimalFormat("0.00");

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.balance);
		categorias = (TextView) findViewById(R.id.balanco_categorias);
		gastos = (TextView) findViewById(R.id.balanco_gastos);
		ganhos = (TextView) findViewById(R.id.balanco_ganhos);
		valorGanhos = (TextView) findViewById(R.id.valor_ganhos);
		saldo = (TextView) findViewById(R.id.saldo);

		debito = DebitoAdapter.getInstance(this);
		credito = CreditoAdapter.getInstance(this);
		categoria = CategoriaAdapter.getInstance(this);

		String retorno = "";
		String valor = "";

		HashMap<String, String> mapaDeb = (HashMap<String, String>) debito
				.getDebitsByType();
		List<String> categorias = categoria.getAllCategorys();
		for (int i = 0; i < categorias.size(); i++) {
			if (mapaDeb.get(categorias.get(i)) != null) {
				retorno += categorias.get(i) + "\n";
				valor += mapaDeb.get(categorias.get(i)) + " R$\n";
			}
		}

		retorno += "Total dos Gastos:\n\n ";
		valor += fmt.format(debito.getDebitsSum()) + " R$\n\n";
		this.categorias.setText((CharSequence) retorno);
		this.gastos.setText((CharSequence) valor);

		retorno = "Ganhos:\n\n";
		valor = fmt.format(credito.getCreditsSum()) + " R$\n\n";
		this.ganhos.setText((CharSequence) retorno);
		this.valorGanhos.setText((CharSequence) valor);

		double valorSaldo = credito.getCreditsSum() - debito.getDebitsSum();
		String saldo = "" + fmt.format(valorSaldo) + " R$\n";
		this.saldo.setText((CharSequence) saldo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_configuration, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.filter) {
			finish();
			startActivity(new Intent(this, DateUpdateActivity.class));
			return true;
		}

		if (item.getItemId() == R.id.redefine_default) {
			redefinirPadrao();
			finish();
			startActivity(new Intent(this, ExtractActivity.class));
			return true;
		}
		return (super.onOptionsItemSelected(item));
	}

	private void redefinirPadrao() {
		ConfigurationAdapter config = ConfigurationAdapter.getInstance(frame);
		config.populate();
	}

	private void selecionarCategoria() {
		List<String> categorias = categoria.getAllCategorys();
		final String[] items = new String[categorias.size()];
		for (int i = 0; i < items.length; i++) {
			items[i] = categorias.get(i);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(frame);
		builder.setTitle("Selecione a Categoria");
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				String categoria = items[item];
				ConfigurationAdapter config = ConfigurationAdapter
						.getInstance(frame);
				String id = config.getId();
				config.updateCategory(categoria, id);
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

}
